<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        session_start();
    }

    public function index()
    {
        
    }

    public function is_logged_in() {
        return isset($_SESSION['username']) && isset($_SESSION['role']) && isset($_SESSION['login']);
    }

    public function username_exists($uname, $role)
    {
        switch ($role) {
            case 1:
                $this->load->model('admin_model');
                if ($this->admin_model->check_username($uname)) {
                    return true;
                } else {
                    $this->form_validation->set_message('username_exists', 'Username does not exist.');
                    return false;
                }
                break;
            case 2:
                $this->load->model('encoder_model');
                if ($this->encoder_model->check_username($uname)) {
                    return true;
                } else {
                    $this->form_validation->set_message('username_exists', 'Username does not exist.');
                    return false;
                }
                break;
            case 3:
                $this->load->model('student_model');
                if ($this->student_model->check_username($uname)) {
                    return true;
                } else {
                    $this->form_validation->set_message('username_exists', 'Username does not exist.');
                    return false;
                }
                break;
            case 4:
                $this->load->model('instructor_model');
                if ($this->instructor_model->check_username($uname)) {
                    return true;
                } else {
                    $this->form_validation->set_message('username_exists', 'Username does not exist.');
                    return false;
                }
                break;
        }
    }

    public function username_not_exists($uname, $role)
    {
        if ($this->is_logged_in()) {
            switch($role) {
                case 2:
                    $this->load->model('encoder_model');
                    if ($this->encoder_model->check_username($uname)) {
                        $this->form_validation->set_message('username_not_exists', 'Username is already taken.');
                        return false;
                    } else {
                        return true;
                    }
                    break;
                case 3:
                    $this->load->model('student_model');
                    if ($this->student_model->check_username($uname)) {
                        $this->form_validation->set_message('username_not_exists', 'Username is already taken.');
                        return false;
                    } else {
                        return true;
                    }
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    if ($this->instructor_model->check_username($uname)) {
                        $this->form_validation->set_message('username_not_exists', 'Username is already taken.');
                        return false;
                    } else {
                        return true;
                    }
                    break;
            }
        } else {
            redirect('login');
        }
    }

    public function stdno_check($stdno)
    {
        if ($this->is_logged_in()) {
            $this->load->model('student_model');
            if ($this->student_model->check_stdno($stdno)) {
                $this->form_validation->set_message('stdno_check', 'Student number is already taken.');
                return false;
            } else {
                return true;
            }
        }
    }

    public function valid_std_no($std_no) {
        if(!preg_match('([0-9]{4}-[0-9]{5})', $std_no)) {
            $this->form_validation->set_message('valid_std_no', 'Invalid student number format.');
            return false; 
        } else {
            return true;
        }
    }

    public function alpha_with_space($str) {
        if(!preg_match("/^([a-z ])+$/i", $str)) {
            $this->form_validation->set_message('alpha_with_space', 'Invalid name format.');
            return false; 
        } else {
            return true;
        }
    }
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */